package com.warm.base.algorithm.structure.book;

/**
 * 二分查找
 */
public class BinarySearch {
    public static int rank(int key, int[] a){
        int lo = 0; // 数组必须是有序的
        int hi = a.length - 1;
        while (lo <= hi){ // 备查找的元素要么不存在，要么必然在 a[lo, hi] 之中
            int mid = lo + (hi - lo)/2;
            if (key < a[mid]) hi = mid -1;
            else if (key > a[mid]) lo = mid + 1;
            else return mid;
        }
        return -1;
    }
}
